// 文件路径: ruoyi-admin/src/main/java/com/ruoyi/web/controller/nongjt/certificate/CertificateController.java
package com.ruoyi.web.controller.nongjt.certificate;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.nongjt.domain.pojo.NjtCertificate;
import com.ruoyi.nongjt.service.NjtCertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 合格证管理控制器（完整 CRUD）
 */
@RestController
@RequestMapping("/nongjt/certificate")
public class CertificateController extends BaseController {

    @Autowired
    private NjtCertificateService njtCertificateService;

    /**
     * 分页查询合格证列表
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:certificate:list')")
    @GetMapping("/list")
    public TableDataInfo list(NjtCertificate njtCertificate) {
        startPage();
        List<NjtCertificate> list = njtCertificateService.getNjtCertificateList(njtCertificate);
        return getDataTable(list);
    }

    /**
     * 获取合格证详细信息（用于编辑/详情页）
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:certificate:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        NjtCertificate certificate = njtCertificateService.getNjtCertificateById(id);
        if (certificate == null) {
            return AjaxResult.error("未找到对应数据");
        }
        return AjaxResult.success(certificate);
    }

    /**
     * 新增合格证
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:certificate:add')")
    @Log(title = "合格证管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody NjtCertificate njtCertificate) {
        // 设置默认值
        if (njtCertificate.getIssueDate() == null) {
            njtCertificate.setIssueDate(new java.util.Date());
        }
        njtCertificate.setPrintTimes(0);
        njtCertificate.setScanTimes(0);
        njtCertificate.setIsDeleted(0); // 未删除
        int rows = njtCertificateService.insertNjtCertificate(njtCertificate);
        return toAjax(rows);
    }

    /**
     * 修改合格证
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:certificate:edit')")
    @Log(title = "合格证管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody NjtCertificate njtCertificate) {
        int rows = njtCertificateService.updateNjtCertificate(njtCertificate);
        return toAjax(rows);
    }

    /**
     * 删除合格证（逻辑删除）
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:certificate:remove')")
    @Log(title = "合格证管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        int rows = njtCertificateService.deleteNjtCertificate(id);
        return toAjax(rows);
    }

    /**
     * 批量删除合格证
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:certificate:remove')")
    @Log(title = "合格证管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteBatch")
    public AjaxResult removeBatch(@RequestBody Long[] ids) {
        int rows = njtCertificateService.deleteNjtCertificateBatch(ids);
        return AjaxResult.success("删除成功：" + rows + " 条");
    }
}
